package com.camemax.rabbitmq.utils;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.util.Map;

public class BindingsModules implements Bindings, Runnable{

    private ConnectionFactory factory = RabbitMQUtils.createFactory();
    private Connection connection = null;
    private Channel channel = null;
    private String queueName;
    private String exchangeName;
    private String routingKey;
    private Map<String, Object> headersMap;

    public BindingsModules(String exchangeName, String queueName, String routingKey, Map<String, Object> headersMap) {
        this.exchangeName = exchangeName;
        this.queueName = queueName;
        this.routingKey = routingKey;
        this.headersMap = headersMap;
    }

    @Override
    public void run() {
        try {
            connection = factory.newConnection();
            channel = connection.createChannel();

            channel.queueBind(queueName, exchangeName, routingKey, headersMap);
            System.out.println("Bindings between [" + exchangeName + "] and [" + queueName + "] with @" + routingKey + " has finished!");
        } catch (Exception e) {
            e.printStackTrace();
        }  finally {
            RabbitMQUtils.close(channel, connection);
        }
    }
}
